package com.coldworks.coldjoke;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

import com.coldworks.base.BaseFragment;
import com.coldworks.coldjoke.R;
import com.coldworks.coldjoke.activity.LoginActivity;
import com.coldworks.coldjoke.adapter.TopicListViewAdapter;
import com.coldworks.coldjoke.manager.RefreshListCallback;
import com.coldworks.coldjoke.manager.TopicsManager;
import com.coldworks.coldjoke.manager.UserManager;
import com.coldworks.coldjoke.manager.UserManager.UserStateSubject;
import com.coldworks.coldjoke.model.TopicModel;
import com.coldworks.coldjoke.ui.MyListView;
import com.coldworks.coldjoke.ui.MyListView.OnRefreshListener;
import com.coldworks.coldjoke.ui.MyListView.OnRefreshOlderListener;
import com.coldworks.coldjoke.ui.TopicView.TVUserStateDelegate;
import com.coldworks.coldjoke.ui.TopicView;
import com.coldworks.coldjoke.util.CONST;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class NavigationTopicFragment extends BaseFragment implements Observer,
		TVUserStateDelegate {

	private MyListView listView;
	private TopicView topicView;
	private ImageButton backBtn;
	private ImageButton menuBtn;
	private ImageButton shareBtn;

	private TopicsManager topicsManager;
	private TopicListViewAdapter topicsListAdapter;
	private List<TopicModel> topicInfos;

	private boolean isShowTopicView = false;

	private OnMenuShowListener onMenuShowListener;
	private boolean isLogin;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			onMenuShowListener = (OnMenuShowListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement onMenuShowListener");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
		topicsManager = new TopicsManager(CONST.TYPE.TEXT_AND_IMAGE);
		topicInfos = topicsManager.getListShow();
		topicsListAdapter = new TopicListViewAdapter(getActivity(), topicInfos);
		userStateSubject = UserManager.getInstance().getUserStateSubject();
		userStateSubject.addObserver(this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(R.layout.navigation_topic, container,
				false);
		menuBtn = (ImageButton) view.findViewById(R.id.button_menu);
		backBtn = (ImageButton) view.findViewById(R.id.button_back);
		shareBtn = (ImageButton) view.findViewById(R.id.button_share);
		listView = (MyListView) view.findViewById(R.id.topic_listview);
		topicView = (TopicView) view.findViewById(R.id.topic_view);
		topicView.addTVUserStateDelegate(this);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		isLogin = userStateSubject.getUserState() == UserManager.LOGINED ? true
				: false;
		listView.setAdapter(topicsListAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				try {
					if (-1 == arg3) {
						return;
					}
					TopicModel jokeGatherInfo = (TopicModel) arg0
							.getItemAtPosition(arg2);
					topicView.setData(jokeGatherInfo);
					showTopicView();
				} catch (Exception e) {
					log.error("", e);
				}
			}
		});

		fetchNewerTopics();

		listView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				fetchNewerTopics();
			}
		});
		listView.setOnRefreshOlderListener(new OnRefreshOlderListener() {

			@Override
			public void onRefresh() {
				fetchOlderTopics();

			}
		});
		menuBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onMenuShowListener.showMenuNavigation();
			}
		});

		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showListView();
			}
		});
		shareBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				topicView.share();
			}
		});
	}

	private UserStateSubject userStateSubject;

	@Override
	public void update(Observable o, Object data) {
		UserStateSubject observable = (UserStateSubject) o;
		if (observable.getUserState() == UserManager.LOGINED) {
			isLogin = true;
			log.info("TopicFragment已注册，并被通知到登陆状态");
		} else {
			isLogin = false;
			log.info("TopicFragment已注册，并被通知到未登陆状态");
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}

	private void fetchNewerTopics() {
		topicsManager.fetchTopicsTask(getActivity(),
				TopicsManager.REFRESHING_NEWER, new RefreshListCallback() {

					@Override
					public void refreshDone(int refreshState) {

						if (refreshState == TopicsManager.REFRESH_SUCC) {
							topicsListAdapter.notifyDataSetChanged();
							listView.onRefreshComplete();
						} else {
							Toast.makeText(getActivity(),
									R.string.home_tosat_refresh_fail,
									Toast.LENGTH_SHORT).show();
						}

					}
				});
	}

	private void fetchOlderTopics() {
		topicsManager.fetchTopicsTask(getActivity(),
				TopicsManager.REFRESHING_OLDER, new RefreshListCallback() {

					@Override
					public void refreshDone(int refreshState) {

						if (refreshState == TopicsManager.REFRESH_SUCC) {
							topicsListAdapter.notifyDataSetChanged();
							listView.onRefreshOlderComplete();
						} else {
							Toast.makeText(getActivity(),
									R.string.home_tosat_refresh_fail,
									Toast.LENGTH_SHORT).show();
						}

					}
				});
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	public boolean isShowTopicView() {
		return isShowTopicView;
	}

	public void showListView() {
		isShowTopicView = false;
		listView.setVisibility(View.VISIBLE);
		topicView.setVisibility(View.INVISIBLE);
		backBtn.setVisibility(ImageButton.GONE);
		shareBtn.setVisibility(View.GONE);
		menuBtn.setVisibility(ImageButton.VISIBLE);
	}

	public void showTopicView() {
		isShowTopicView = true;
		shareBtn.setVisibility(View.VISIBLE);
		listView.setVisibility(View.INVISIBLE);
		topicView.setVisibility(View.VISIBLE);
		backBtn.setVisibility(ImageButton.VISIBLE);
		menuBtn.setVisibility(ImageButton.GONE);
	}

	@Override
	public void TVUScomment() {
		if (isLogin) {
			topicView.comment();
		} else {
			onMenuShowListener.showMenuLogin();
		}
	}

	@Override
	public void TVUSfavorite() {
		// TODO Auto-generated method stub

	}

	@Override
	public void TVUScell() {
		// TODO Auto-generated method stub

	}

}
